#!/bin/bash

UBUNTU_RELEASE=`lsb_release -rs`
if [[ "${UBUNTU_RELEASE}" == "18.04" ]]; then
    echo "${UBUNTU_RELEASE}"
    ROS_VERSION="eloquent"
elif [[ "${UBUNTU_RELEASE}" == "20.04" ]]; then
	echo "${UBUNTU_RELEASE}"
	ROS2_VERSION="galactic"
fi

echo 'using tsinghua source'
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros1-latest.list'
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
# curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
curl -s https://gitcode.net/mirrors/ros/rosdistro/-/raw/master/ros.asc | sudo apt-key add -
sudo apt update


sudo apt install ros-$ROS_VERSION-desktop -y
sudo apt install python3-rosdep -y
sudo rosdep init
rosdep update


sudo apt install python3-colcon-common-extensions -y

echo "source /opt/ros/$ROS2_VERSION/setup.zsh" >> ~/.zshrc
echo "# argcomplete for ros2 & colcon" >> ~/.zshrc
echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.zsh" >> ~/.zshrc
echo "eval \"\$(register-python-argcomplete3 ros2)\"" >> ~/.zshrc
echo "eval \"\$(register-python-argcomplete3 colcon)\"" >> ~/.zshrc


echo "export ROS_LOCALHOST_ONLY=1" >> ~/.zshrc
source ~/.zshrc

sudo apt install -y ros-eloquent-ros2bag ros-eloquent-rosbag2*